package syan.rediss.liststener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;


/**
 * @author CHENTAIWEN
 * @create 2022/8/4 9:34
 */
@Slf4j
@Component
public class ListenerMessage implements StreamListener<String, MapRecord<String, String, String>> {

    @Value("${redis.Group}")
    private String groupId;

    private static int count = 0;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
        @Override
    public void onMessage(MapRecord<String, String, String> message) {
            try {
//            将获取到的信息进行消费
                log.error("消费redis消息：消息id={}, 消息value={}", message.getId(), message.getValue());
                Thread.sleep(0);
//                    ack确认消费
                redisTemplate.opsForStream().acknowledge(groupId,message);
            } catch (InterruptedException e) {

            }
        }
}
